package com.e.ticket;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.e.Dao.UserDao;
import com.e.SQL.UserDBHepler;

public class Login extends AppCompatActivity {

    TextView tv_user,tv_password;
    Button bt_1;
    final String FILE_NAME="user";
    private UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        tv_user = (TextView)findViewById(R.id.username);
        tv_password = (TextView)findViewById(R.id.password);
        bt_1 = (Button) findViewById(R.id.btn_login);
        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userDao = new UserDao(new UserDBHepler(Login.this));
                String sno = tv_user.getText().toString();
                String pwd = tv_password.getText().toString();
                if(userDao.findUserByName(sno,pwd)){
                    SharedPreferences sharedPreferences = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("sno",sno );
                    editor.putString("pwd", pwd);
                    editor.commit();
                    Intent intent = new Intent(Login.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(Login.this,"账号或密码错误!",Toast.LENGTH_SHORT).show();
                }

            }
        });



    }
}